/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.dao.pcds;

import br.gov.cemaden.library.dao.GenericDao;
import br.gov.cemaden.library.dao.produto.ProdutoDao;
import br.gov.cemaden.library.interfaces.pcds.RedeInterface;
import br.gov.cemaden.library.model.pcds.Rede;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author paulo.dias
 */
public class RedeDao extends GenericDao implements RedeInterface {

    private Session session;

    public RedeDao(Session session) {
        this.session = session;
    }

    @Override
    public List<Rede> findAll() {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Rede> pcdsRede = null;
        try {
            Query query = session.getNamedQuery("Rede.findAll");
            pcdsRede = (List<Rede>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no RedeDao.findAll: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return pcdsRede;

    }

    @Override
    public Rede findByIdRede(Long idRede) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Rede pcdsRede = null;
        try {
            Query query = session.getNamedQuery("Rede.findByIdRede").setParameter("idRede", idRede);
            pcdsRede = (Rede) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no RedeDao.findByIdRede: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return pcdsRede;
    }

    @Override
    public List<Rede> findByNome(String nome) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Rede> pcdsRede = null;
        try {
            Query query = session.getNamedQuery("Rede.findByNome").setParameter("nome", nome);
            pcdsRede = (List<Rede>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no RedeDao.findByNome: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return pcdsRede;
    }

    @Override
    public List<Rede> findBySigla(String sigla) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Rede> pcdsRede = null;
        try {
            Query query = session.getNamedQuery("Rede.findBySigla").setParameter("sigla", sigla);
            pcdsRede = (List<Rede>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no RedeDao.findBySigla: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return pcdsRede;
    }

    @Override
    public List<Rede> findByPercMinFuncto(Short percMinFuncto) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Rede> pcdsRede = null;
        try {
            Query query = session.getNamedQuery("Rede.findByPercMinFuncto").setParameter("percMinFuncto", percMinFuncto);
            pcdsRede = (List<Rede>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no RedeDao.findByPercMinFuncto: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return pcdsRede;
    }

    @Override
    public List<Rede> findByTempoMaxSemAquisicao(Integer tempoMaxSemAquisicao) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Rede> pcdsRede = null;
        try {
            Query query = session.getNamedQuery("Rede.findByTempoMaxSemAquisicao").setParameter("tempoMaxSemAquisicao", tempoMaxSemAquisicao);
            pcdsRede = (List<Rede>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no RedeDao.findByTempoMaxSemAquisicao: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return pcdsRede;
    }

    @Override
    public void insert(Rede pcdsRede) {
        try {
            saveOrUpdate(this.session, pcdsRede);
        } catch (Exception ex) {
            Logger.getLogger(ProdutoDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void update(Rede pcdsRede) {
        try {
            saveOrUpdate(this.session, pcdsRede);
        } catch (Exception ex) {
            Logger.getLogger(ProdutoDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void delete(Rede pcdsRede) {
        try {
            delete(pcdsRede);
        } catch (Exception ex) {
            Logger.getLogger(ProdutoDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
